$(document).ready(function() {
	msgBootLoader.init();
});
var msgBootLoader = {
	
	init: function(){
		$('.msgHolder').click(function(){
			$('.msgHolder').removeClass('activeMsg');
			var getStatus = $('.msgHolder').attr('data-role');
			var getID = $(this).attr('id');
			var splitID = getID.split('_');
			var activeMsg = 'msg_'+splitID[1];
			if(getID==activeMsg)
			{
				$('#msg_'+splitID[1]).addClass('activeMsg');
			}
			
			$('#loader_'+splitID[1]).show();
			setTimeout(function(){
				msgBootLoader.loadMsg(splitID[1]);
			}, 3000);
		});
		
		$('.delMsgHolder').click(function(){
			var msgDelId = $(this).attr('data-role');
			$('li#msg_'+msgDelId).text();
			$('li#msg_'+msgDelId).remove('li#msg_'+msgDelId);
			msgBootLoader.deleteMsg(msgDelId);
		});
	},
	loadMsg: function(intMsgID){
		uri = 'dashboard/get_sent_message';
		data = {
			'intMsgID' : intMsgID
		};
		$.ajax({
			type: "POST",
			url: base_url+uri,
			data: data,
			dataType: 'json',
			success: function(result){
				if(result.strMiddleName!='')
				{
					var strMidName = result.strMiddleName+' ';
				}
				else
				{
					var strMidName = '';
				}
				html = '';
				html += '<div class="msgHolder">';
					html += '<table class="table table-bordered">';
						html += '<tr>';
							html += '<td style="width:62px !important"><strong>To:</strong></td>';
							html += '<td><a href="'+base_url+'dashboard/account_settings/'+result.strFrom+'">';
								html += '<strong>'+result.strFirstName+' '+strMidName+''+result.strLastName+'</strong>';
							html += '</a>';
							html += ' &bull; <span class="rankSender">'+result.strRank+'</span>';
							html += '<p class="dateSend">'+result.dateCreated+'</p></td>';
						html += '<tr>';
							html += '<td style="width:62px !important"><strong>Message:</strong></td>';
							html += '<td colspan="2"><p>'+result.strMsgBody+'</p></td>';
						html += '<tr>';
						html += '</tr>';
					html += '</table>';
				html += '</div>';
				$('#msgBodyContent').html(html);
			},
			complete: function(){
				$('#loader_'+intMsgID).hide();
			}
		});
	},
	deleteMsg: function(intMsgID){
		uri = 'dashboard/delete_sent_msg';
		data = {
			'intMsgID' : intMsgID
		};
		$.ajax({
			type: "POST",
			url: base_url+uri,
			data: data,
			dataType: 'json',
			success: function(result){
				
			}
		});
	}
}